package com.njitzx;

import com.njitzx.entity.po.Member;
import com.sun.org.apache.xerces.internal.xs.XSFacet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileOutputStream;

@SpringBootTest
public class test {

    @Test
    void test() throws Exception {
        FileInputStream inputStream = new FileInputStream("E:\\NJITZX大三上\\第二阶段\\project\\会员信息表（模板）.xlsx");

        XSSFWorkbook excel = new XSSFWorkbook();
        XSSFSheet sheet = excel.createSheet("njitzx");

        XSSFRow row1 = sheet.createRow(0); //第一行
        row1.createCell(1).setCellValue("姓名");
        row1.createCell(2).setCellValue("城市");

        XSSFRow row2 = sheet.createRow(1);
        row2.createCell(1).setCellValue("张三");
        row2.createCell(2).setCellValue("北京");

        XSSFRow row3 = sheet.createRow(2);
        row3.createCell(1).setCellValue("张三");
        row3.createCell(2).setCellValue("北京");

        FileOutputStream out = new FileOutputStream("E:\\NJITZX大三上\\第二阶段\\project\\会员信息.xlsx");
        excel.write(out);
        out.close();
        excel.close();
        inputStream.close();

    }

    //读取文件
    @Test
    void test1() throws Exception {
        FileInputStream inputStream = new FileInputStream("E:\\NJITZX大三上\\第二阶段\\project\\会员信息表（模板）.xlsx");
        //创建一个工作部
        XSSFWorkbook excel = new XSSFWorkbook(inputStream);

        //拿到第一张工作表
        XSSFSheet sheet=excel.getSheetAt(0);
        int lastRowNum =sheet.getLastRowNum();
        //创建member对象
        Member member = new Member();

        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i); //拿到行
            String username = row.getCell(0).getStringCellValue();
            String email = row.getCell(1).getStringCellValue();
            String sex = row.getCell(2).getStringCellValue();
            String qq = row.getCell(3).getStringCellValue();
            String phone = row.getCell(4).getStringCellValue();
            String education = row.getCell(5).getStringCellValue();
            String hobby = row.getCell(6).getStringCellValue();
            String descp = row.getCell(7).getStringCellValue();

            member.setUsername(username);
            member.setEmail(email);
            member.setGender(sex);
            member.setQq(qq);
            member.setPhone(phone);
            member.setEducation(education);
            member.setHobby(hobby);
            member.setIntroduce(descp);
            System.out.println(member);
        }
    }
}
